Shorthand Conditions

Using the "?" operator, it is possible to significantly shorten the code necessary for certain types of conditional statements. For example, the following statements...

if (calculationType == 'multiply') {
	a = b * c - d;
} else {
	a = b / c - d;
}

...could be reduced to just...

a = (calculationType == 'multiply') ? (b * c - d) : (b / c - d);

...and can be reduced even a little further to...

a = ((calculationType == 'multiply') ? (b * c) : (b / c)) - d;

...since the subtraction of the variable "d" is common to both expressions. It is even possible to use the "?" operator in cases where the result of the two options for the "?" expression are not to be assigned to anything. For example, the statements...

if (useGroovoid) {
	Music.play ('UI-Click1');
} else {
	Music.playNote (1,1,175,77,127,1000);
}

...could be reduced to...

useGroovoid ? music.play ('UI-Click1') : music.playNote (1,1,175,77,127,1000);